<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 9/19/14
 * Time: 10:10 PM
 */

namespace Backend\Modules\Video\Forms;


use JsonSchema\Constraints\String;
//use Phalcon\Forms\Form;
use Common\Forms\BaseForm;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Radio;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Identical;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\Confirmation;
use Phalcon\Validation\Validator\Between;
use Phalcon\Validation\Validator\Url as UrlValidator;
use Common\Library\Common as LibraryCommon;
use Backend\Modules\Video\Models\VideoFile;

class FormCreateVideoFile extends BaseForm
{
    public function initialize($entity = null , $options = null)
    {
        $id = new Hidden('id');
        $this->add($id);

        $name = new Text('name' , array(
            'class' => 'form-control col-xs-12',
        ));
        $name->setLabel("Tên file");

        $name->addValidators(array(
            new PresenceOf(array(
                'message' => 'name is required'
            )),
            new  StringLength(array(
                'max' => 150 ,
                'message' => 'name is too long'
            ))
        ));
        $this->add($name);

        $source = new Select('source_server' ,
            LibraryCommon::listSourceFile() ,
            array(
                'useEmpty' => true ,
                'emptyText' => 'Chọn source file video' ,
                'emptyValue' => '' ,
                'class' => 'form-control col-xs-12',
            )
        );
        $source->addValidator(new PresenceOf(array(
            'Message' => 'souce is required'
        )));
        $source->setLabel('Chọn nguồn video');
        $this->add($source);

        $linkSource = new Text('link_source_server' , array(
            'class' => 'form-control col-xs-12',
        ));
        $linkSource->setLabel("Link source file");

        $linkSource->addValidators(array(
            new PresenceOf(array(
                'message' => 'Link source is required'
            )),
            new  StringLength(array(
                'max' => 250 ,
                'message' => 'Link source is too long'
            )),
            /*new UrlValidator(array(
                'message' => 'Url is not valid'
            )),*/
        ));
        $this->add($linkSource);

        $quanlity = new Text('quanlity' , array(
            'class' => 'form-control col-xs-12',
            'value' => 0,
        ));
        $quanlity->setLabel("Chất lượng video");
        $this->add($quanlity);

        $quanlityDescription = new Text('quanlity_description' , array(
            'class' => 'form-control col-xs-12',
        ));
        $quanlityDescription->setLabel("Mô tả chất lượng video");
        $this->add($quanlityDescription);

        $filePath = new Text('file_path' , array(
            'class' => 'form-control col-xs-12',
        ));
        $filePath->setLabel("Đường dẫn video file");
        $this->add($filePath);

        $fileExtension = new Text('file_extension' , array(
            'class' => 'form-control col-xs-12',
        ));
        $fileExtension->setLabel("Đuôi file video");
        $this->add($fileExtension);

        $protocol = new Select('protocol' ,
            LibraryCommon::listProtocol() ,
            array(
                'useEmpty' => true ,
                'emptyText' => 'Chọn giao thức' ,
                'emptyValue' => '' ,
                'class' => 'form-control col-xs-12',
            )
        );
        $protocol->setLabel("Giao thức");
        $this->add($protocol);

        $agent = new Select('agent' ,
            LibraryCommon::listAgentVideo() ,
            array(
                'useEmpty' => true ,
                'emptyText' => 'Chọn source file video' ,
                'emptyValue' => '' ,
                'class' => 'form-control col-xs-12',
            )
        );
        $agent->setLabel("Thiết bị");
        $this->add($agent);

        $status = new Select('status' ,
            array(
                'A' => 'Hoạt động' ,
                'H' => 'Ẩn' ,
                'D' => 'Vô hiệu hóa'
            ) ,
            array(
                'useEmpty' => true ,
                'emptyText' => 'Chọn trạng thái' ,
                'emptyValue' => '' ,
                'class' => 'form-control col-xs-12',
            )
        );
        $status->setLabel('Chọn trạng thái');
        $status->addValidator(new PresenceOf(array(
            'Message' => 'Status is required'
        )));
        $this->add($status);


        // CSRF
        $csrf = new Hidden('csrf' , array(
            'value' => $this->security->getToken()
        ));

        /*$csrf->addValidator(new Identical(array(
            'value' => $this->security->getSessionToken(),
            'message' => 'CSRF validation failed'
        )));*/

        $this->add($csrf);

        // Sign Up
        $this->add(new Submit('submit', array(
            'class' => 'btn btn-primary'
        )));

    }

    public function messages($name)
    {
        if($this->getMessagesFor($name))
        {
            foreach ($this->getMessagesFor($name) as $message)
            {
                $this->flash->error($message);
            }
        }
    }

}
